package net.bolue.console.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.bolue.console.data.InsertInfo;
import net.bolue.console.data.model.InsertInfoModel;
import net.bolue.sysmng.db.DbCreateSqlUtil;
import net.bolue.sysmng.db.DbOpUtil;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.sysmng.model.ColInfoModel;
import net.bolue.sysmng.tblbean.ColInfo;
import net.bolue.sysmng.tblbean.InsRes;
import net.bolue.sysmng.util.ReflectHelper;
import net.bolue.web.mvc.ActionResult;
import net.bolue.web.util.RequestUtil;

/**
 * 新增操作业务类
 * @author Administrator
 *
 */
public class InsertModel extends BusiBase{

	private String insertSign;//新增单元标识
	private InsertInfo insertInfo;
	private ColInfoModel colInfoModel;
	private InsertInfoModel insertInfoModel;
	
	public InsertModel(){};
	
	public InsertModel(HttpServletRequest request,InsertInfo insertInfo)throws BolueException{
		super(request);
		this.insertInfo = insertInfo;
	}
	
	public InsertModel(HttpServletRequest request,InsertInfo insertInfo,String insertSign)throws BolueException{
		super(request);
		this.insertInfo = insertInfo;
		this.insertSign = insertSign;
	}
	
	public InsertModel(HttpServletRequest request,InsRes insRes)throws BolueException{
		super(request);
		insertInfoModel = new InsertInfoModel(request,insRes);
		this.insertInfo = insertInfoModel.getInsertInfo();
	}
	
	/**
	 * 初始化表列信息
	 * @throws BolueException
	 */
	private void init()throws BolueException{
		if(this.insertInfo==null){
			return;
		}
		InsRes insRes = this.insertInfo.getInsRes();
		if(insRes==null){
			return;
		}
		List<ColInfo> colInfoList = this.insertInfo.getColInfoList();
		if(colInfoList==null){
			if(colInfoModel==null){
				colInfoModel = new ColInfoModel();
			}
			String tableName = insRes.tableName();
			colInfoList = colInfoModel.getColInfoList(insRes.getSubSys(), tableName);
			this.insertInfo.setColInfoList(colInfoList);
		}
		if(colInfoList==null){
			return;
		}
	}
	
	/**
	 * 插入数据主方法
	 * @return
	 * @throws BolueException
	 */
	public ActionResult insert()throws BolueException{
		this.init();
		InsRes insRes = this.insertInfo.getInsRes();
		Integer insType = insRes.getInsType();
		if(insType==1){
			return insertByTable();
		}else if(insType==3){//批量直接数据表操作
			
		}
		return null;
	}
	
	/**
	 * 直接数据表插入
	 * @return
	 * @throws BolueException
	 */
	public ActionResult insertByTable()throws BolueException{
		InsRes insRes = this.insertInfo.getInsRes();
		String subSys = insRes.getSubSys();
		String dbResId = subSys;
		String table = insRes.getTableName();
		putInsertData();
		
		boolean result = false;
		if(isUseBusi()){
			busiInsert();
		}else{
			DbOpUtil dbOpUtil = new DbOpUtil(dbResId);
			Map<String,Object> insertMap = this.insertInfo.getInsertMap();
			dbOpUtil.insert(table, insertMap);
		}
		
		ActionResult ActionResult = new ActionResult();
		result = true;
		ActionResult.setResult(result);
		return ActionResult;
	}
	
	/**
	 * 使用业务类插入
	 * @throws BolueException
	 */
	public void busiInsert() throws BolueException{
		BusiBase busiBase = null;
		InsRes insRes = this.insertInfo.getInsRes();
		String busiClass = insRes.getBusiClass();
		Object localObject = ReflectHelper.classNewInstance(busiClass);
		if(localObject!=null&&(localObject instanceof BusiBase)){
			busiBase = (BusiBase)localObject;
		}
		String busiMethod = insRes.getBusiMethod();
		busiMethod = busiMethod + "(java.util.Map)";
		Object[] paramArray = new Object[1];
		Map<String,Object> insertMap = this.insertInfo.getInsertMap();
		paramArray[0] = insertMap;
		ReflectHelper.invokeMethod(busiBase, busiMethod, paramArray);
	}
	
	public ActionResult batchUpdateByTable()throws BolueException{
		initBatch();
		InsRes insRes = this.insertInfo.getInsRes();
		String[] insColArray = this.insertInfo.getInsColArray();
		if(insColArray==null||insColArray.length<1){
			return null;
		}
		String updateSql = DbCreateSqlUtil.createInsertSql(insRes.getTableName(), insColArray);
		if(updateSql==null){
			return null;
		}
		Integer dataSize = 0;
		String parPre = "";
		if(this.insertSign!=null){
			parPre = this.insertSign + "_";
		}
		String _recordNum = request.getParameter(parPre+"recordNum");
		if(_recordNum!=null){
			dataSize = Integer.parseInt(_recordNum);
		}
		List<Object[]> dataList = new ArrayList<Object[]>();
		Integer cellNum = insColArray.length;
		for(int i=0;i<dataSize;i++){
			Object[] record = new Object[cellNum];
			for(int j=0;j<cellNum;j++){
				for(String insCol : insColArray){
					String colValue = request.getParameter(parPre+"col_"+insCol);
					if(colValue!=null){
						String[] colValueA = colValue.split(",");
						if(colValueA.length>i){
							record[j] = colValueA[i];
						}
					}
					j++;
				}
			}
			dataList.add(record);
		}
		
		String subSys = insRes.getSubSys();
		String dbResId = subSys;
		DbOpUtil dbOpUtil = new DbOpUtil(dbResId);
		dbOpUtil.update(updateSql, dataList);
		
		ActionResult actionResult = new ActionResult();
		boolean result = true;
		actionResult.setResult(result);
		return actionResult;
	}
	
	private void initBatch(){
		InsRes insRes = this.insertInfo.getInsRes();
		String insCols = insRes.getInsCols();
		String[] insColArray = new String[]{};
		if(insCols!=null){
			insColArray = insCols.split(",");
		}
		this.insertInfo.setInsColArray(insColArray);
	}
	
	/**
	 * 获得要插入的数据
	 */
	private void putInsertData(){
		InsRes insRes = this.insertInfo.getInsRes();
		if(insRes==null){
			return;
		}
		Map<String,Object> insertMap = new HashMap<String,Object>();
		String insCols = insRes.getInsCols();
		Map<String,ColInfo> colInfoMap = this.insertInfo.getColInfoMap();
		if(insCols!=null&&colInfoMap!=null){
			String[] insColArray = insCols.split(",");
			if(insColArray!=null&&insColArray.length>0){
				for(int i=0;i<insColArray.length;i++){
					String insCol = insColArray[i];
					String colParamName = "col_"+insCol;
					if(this.insertSign!=null){
						colParamName = this.insertSign+"_"+colParamName;
					}
					String insColValue = RequestUtil.getParamValue(request, colParamName);
					ColInfo colInfo = colInfoMap.get(insCol);
					Object parseValue =  RequestUtil.parseValue(colInfo.getJType(),insColValue);
					insertMap.put(insCol, parseValue);
				}
			}
		}
		this.insertInfo.setInsertMap(insertMap);
	}
	
	/**
	 * 是否使用业务类
	 * @return
	 */
	private boolean isUseBusi(){
		InsRes insRes = this.insertInfo.getInsRes();
		String busiClass = insRes.getBusiClass();
		String busiMethod = insRes.getBusiMethod();
		if(busiClass!=null&&!"".equals(busiClass)&&busiMethod!=null&&!"".equals(busiMethod)){
			return true;
		}
		return false;
	}
}
